package org.bigdata.framework.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

/**
 * xml相关工具类
 * 
 * @author cuichao703
 * 
 */
public class XMLUtils {

	private static final Logger LOG = Logger.getLogger(XMLUtils.class);

	/**
	 * 转化为绝对路径
	 * 
	 * 可以设置系统变量conf.dir
	 * 
	 * 
	 * @return
	 * @throws FileNotFoundException
	 */
	public static String convertToAbsolutePath(String _fileName) {
		Validate.notEmpty(_fileName);

		String dir = System.getProperty("conf.dir");
		String separator = File.separator;

		if (StringUtils.isNotBlank(dir)) {
			File file = new File(dir + separator + _fileName);
			if (file.exists()) {
				return dir + separator + _fileName;
			} else {
				return XMLUtils.class.getResource("/" + _fileName).toString();
			}
		} else {
			return XMLUtils.class.getResource("/" + _fileName).toString();
		}
	}

	/**
	 * 转化为输入流
	 * 
	 * 可以设置系统变量conf.dir
	 * 
	 * @return
	 * @throws FileNotFoundException
	 */
	public static InputStream convertToInputStream(String _fileName)
			throws FileNotFoundException {
		Validate.notEmpty(_fileName);

		String dir = System.getProperty("user.dir");
		String separator = File.separator;

		if (StringUtils.isNotBlank(dir)) {
			File file = new File(dir + separator + _fileName);
			if (file.exists()) {
				return new FileInputStream(file);
			} else {
				return XMLUtils.class.getResourceAsStream("/" + _fileName);
			}
		} else {
			return XMLUtils.class.getResourceAsStream("/" + _fileName);
		}
	}

	// /**
	// * 转化为URL
	// * @return
	// * @throws MalformedURLException
	// */
	// public static URL convertToURL(String _fileName) {
	// Validate.notEmpty(_fileName);
	//
	// String dir= System.getProperty("conf.dir");
	// String separator=File.separator;
	// if (StringUtils.isNotBlank(dir)) {
	// String path = dir+separator+_fileName;
	// try {
	// return new URL(path);
	// } catch (MalformedURLException e) {
	// e.printStackTrace();
	// }
	// }else {
	// return XMLUtils.class.getResource("/"+_fileName);
	// }
	// return null;
	// }

	/**
	 * 转化为Document对象
	 * 
	 * @return
	 */
	public static Document convertToDocument(String _fileName) {
		Validate.notEmpty(_fileName);

		// 声明SAXReader
		SAXReader saxReader = new SAXReader();
		Document document = null;
		try {
			document = saxReader.read(convertToInputStream(_fileName));
		} catch (Exception e) {
			LOG.error(UtilConstant.PROJECT + " 转化为Document对象失败,文件名:"
					+ _fileName, e);
			throw new RuntimeException(e);
		}
		return document;
	}

	/**
	 * 转化为Document对象
	 * 
	 * @return
	 */
	public static Document convertToDocument(File _file) {
		Validate.notNull(_file);

		// 声明SAXReader
		SAXReader saxReader = new SAXReader();
		Document document = null;
		try {
			document = saxReader.read(_file);
		} catch (Exception e) {
			LOG.error(
					UtilConstant.PROJECT + " 转化为Document对象失败,文件名:"
							+ _file.getName(), e);
			throw new RuntimeException(e);
		}
		return document;
	}
}
